//
//  ComicShowViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/15.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "ComicShowViewController.h"
#import <SDWebImageManager.h>
#import "UIImageView+WebCache.h"
#import "ComicShowTableViewCell.h"

#import "ReadPage/comicMenuViewController.h"


@interface ComicShowViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ComicShowViewController
{
    NSMutableArray* _dataSource;
    UITableView* _comicShow;
    NSString* _finish;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://japi.juhe.cn/comic/book?key=e4c2c6f92b486c97e01328b1bc54c913" params:
  @{@"name":@"",
    @"type":self.comicType,
    @"skip":@"",
    @"finish":@"",
    @"key":@"e4c2c6f92b486c97e01328b1bc54c913"} success:^(id success) {
        
        NSLog(@"comicSHOW%@",success);
        _dataSource = [[success objectForKey:@"result"] objectForKey:@"bookList"];
        NSLog(@"_comicData:%@",_dataSource);
        [_comicShow reloadData];
    } failure:^(id failure) {
        
    }];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = self.comicType;
    
    [self lazyMod];
    [self.view addSubview:_comicShow];
    
    
}
#pragma mark 懒加载模式
- (UITableView* )lazyMod
{
    if (_comicShow == nil) {
        _comicShow = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
        _comicShow.dataSource = self;
        _comicShow.delegate = self;
        [_comicShow registerNib:[UINib nibWithNibName:NSStringFromClass([ComicShowTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ruse"];
    }
    return _comicShow;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
    //return 1;
}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* ruse = @"ruse";
    ComicShowTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ruse];
    if (cell == nil) {
        cell = [[ComicShowTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ruse];
    }
    
    
    
    
    
    NSDictionary* indexDic = _dataSource[indexPath.row];
    
    NSString* des = [NSString stringWithFormat:@"%@",[indexDic objectForKey:@"des"]];
    NSLog(@"desu!%@",des);
    
    [cell.coverImage sd_setImageWithURL:[indexDic objectForKey:@"coverImg"] placeholderImage:[UIImage imageNamed:@"personBack.jpg"]];
    
    cell.bookName.text = [indexDic objectForKey:@"name"];
//    NSNumber* lastDate = [indexDic objectForKey:@"lastUpdate"];
    NSString* lastUpdate = [NSString stringWithFormat:@"%@",[indexDic objectForKey:@"lastUpdate"] ];
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    [formatter setDateFormat:@"yyyyMMdd"];
    
    NSDate* date = [formatter dateFromString:lastUpdate];
    
    NSDateFormatter* outFormatter = [[NSDateFormatter alloc]init];
    [outFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    [outFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString* time = [outFormatter stringFromDate:date];
    
    cell.lastUpdate.text = [NSString stringWithFormat:@"最后更新:%@",time];
    cell.bookType.text = [NSString stringWithFormat:@"漫画类型:%@",[indexDic objectForKey:@"type"]];
    
    BOOL finish = [[indexDic objectForKey:@"finish"] boolValue];
    NSLog(@"是否完结!%@",finish?@"1":@"0");
    if (finish) {
        _finish = @"完结";
    }
    else
    {
        _finish = @"连载中";
    }
    
    cell.bookInfo.text = [NSString stringWithFormat:@"地域:%@,连载状态:%@",
                           [indexDic objectForKey:@"area"],_finish];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 106;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary* indexDic = _dataSource[indexPath.row];
    NSString* des = [NSString stringWithFormat:@"%@",[indexDic objectForKey:@"des"]];
    NSLog(@"%@",des);
    NSString* comicName = [indexDic objectForKey:@"name"];
    comicMenuViewController* cmVC = [[comicMenuViewController alloc]init];
    cmVC.nameTitle = comicName;
    cmVC.comicSynopsis = des;
    cmVC.coverImage = [indexDic objectForKey:@"coverImg"];
    
    [self.navigationController pushViewController:cmVC animated:YES];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
